package com.woniuxy.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.MenuMapper;
import com.woniuxy.dao.RoleMapper;
import com.woniuxy.dao.UserMapper;

import com.woniuxy.pojo.Menu;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;

import com.woniuxy.util.DataGridView;
import com.woniuxy.util.TreeNode;
import com.woniuxy.util.TreeNodeBuilder;
import com.woniuxy.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<User> findAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public User login(String phone, String lpass) {
        return userMapper.selectUserByTelAndPwd(phone,lpass);
    }

    @Override
    public User findUserByTel(String phone) {
        return this.userMapper.selectUserByTel(phone);
    }

    @Override
    public Set<String> selectRoleNamesByUsid(int usid) {
        return roleMapper.selectRoleNamesByUsid(usid);
    }

    @Transactional
    @Override
    public boolean addUser(User user) {
        boolean flag = false;
        int count=this.userMapper.insertUser(user);
        if (count > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Set<String> selectUrlsByUsid(int usid) {
        return menuMapper.selectUrlsByUsid(usid);
    }

    @Override
    public List<TreeNode> selectMenusByUsid(int usid) {
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        List<Menu> menuList=this.menuMapper.selectMenusByUsid(usid);
        for(Menu menu:menuList){
            TreeNode treeNode = new TreeNode();
            treeNode.setId(menu.getId());
            treeNode.setPid(menu.getPid());
            treeNode.setTitle(menu.getName());
            treeNode.setHref(menu.getUrl());
            treeNode.setSpread(menu.getOpen() == 1 ? true : false);
            treeNodes.add(treeNode);
        }
        return TreeNodeBuilder.build(treeNodes, "L1");
    }

    @Override
    public DataGridView selectUsersByPage(UserVo userVo) {
        Page<Object> page = PageHelper.startPage(userVo.getPage(), userVo.getLimit());
        List<User> userList = this.userMapper.selectUsersByCondition(userVo);
        return new DataGridView(page.getTotal(), userList);
    }

    @Transactional
    @Override
    public void removeUserById(int id) {
        this.userMapper.deleteUserRoleByUsid(id);
        this.userMapper.deleteUserById(id);
    }

    @Transactional
    @Override
    public void removeBatchUser(Integer[] ids) {
        if (ids.length > 0) {
            for (Integer id : ids) {
                this.removeUserById(id);
            }
        }
    }

    /**
     * 重新分配角色
     * @param usid
     * @param rids
     */
    @Transactional
    @Override
    public void grantRole(int usid, int[] rids) {
        this.userMapper.deleteUserRoleByUsid(usid);
        if (rids != null && rids.length > 0) {
            for (Integer rid : rids) {
                this.userMapper.insertUserRole(usid, rid);
            }
        }
    }

    @Override
    public User selectUserById(int id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User selectUserByRname(String rname) {
        return userMapper.selectUserByRname(rname);
    }

    @Override
    public int changePassword(String rname, String lpass) {
        return userMapper.changePassword(rname,lpass);
    }
}
